package binaryTreeNode;

public class DiameterOfBinaryTree {
    public  static int ans;
    public static void main(String[] args) {
        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(2);
//        root.right = new TreeNode(3);
        root.left.left = new TreeNode(4);
        root.left.right = new TreeNode(5);
        root.left.right.left = new TreeNode(6);
        root.left.left.left = new TreeNode(7);
        System.out.println(diameterOfBinaryTree(root));
    }
    public static  int diameterOfBinaryTree(TreeNode root) {
        if (root == null||( root.left == null && root.right == null))
            return 0;
        ans = 1;
        maxDepth(root);
        return ans;
    }
    //从下到上
    public static int maxDepth(TreeNode root) {
        if(root ==null)
            return 0;
        int left = maxDepth(root.left);
        int right = maxDepth(root.right);
        // 求出最大路径TODO
        ans = Math.max(ans,left+right);
        return Math.max(left,right)+1;
    }
}
